<?php

namespace App\Admin\Controllers\UserAddress;

use App\Http\Controllers\Controller;
use App\Model\User_address;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class UserAddressController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('用户地址管理');

            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User_address::class, function (Grid $grid) {
            $grid->user_address_id('ID')->sortable();
            $grid->user("所属用户")->display(function ($user) {
                return "" . $user["nickname"] . "";

            });
//            $grid->province('省份');
//            $grid->city('市');
//            $grid->county('县');
            $grid->address('地址');
            $grid->phone('电话');
            $grid->get_name('收件人姓名');
            $grid->is_default("属性")->display(function ($sex) {
                if ($sex == 1)
                    return "默认地址";
                else
                    return "";
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户');
            $content->description('用户地址管理');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User_address::class, function (Form $form) {
            $form->display('user_address_id', 'user_address_id');
            $form->display('user_id', 'user_id');
            $form->display('province', 'province');
            $form->display('city', 'city');
            $form->display('county', 'county');
            $form->display('address', 'address');
            $form->display('phone', 'phone');
            $form->display('get_name', 'get_name');
            $form->display('is_default', 'is_default');
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('用户地址管理');

            $content->body($this->form());
        });
    }
}
